package com.glsc.ngateway.opmanage.service.deposit;

import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.base.domain.oracle.xinyizj.FsFundSettleInst;
import com.glsc.ngateway.common.base.repo.oracle.xinyizj.FsFundSettleInstRepository;
import com.glsc.ngateway.opmanage.utils.SpecificationUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

@Service
public class FsFundSettleInstService {


    @Resource
    private FsFundSettleInstRepository fsFundSettleInstRepository;


    /**
     * 根据付款方银行账号和划款日期查询所有的指令信息
     *
     * @param payBankAcc
     * @return
     */
    public List<FsFundSettleInst> listByPayBankAcc(String payBankAcc, Long ftDate) {
        if (StringUtils.isEmpty(payBankAcc)) {
            return null;
        }
        if (Objects.isNull(ftDate)) {
            return null;
        }

        FsFundSettleInst carryInst = FsFundSettleInst.builder().payBankAcc(payBankAcc).ftDate(ftDate).build();
        List<FsFundSettleInst> list = fsFundSettleInstRepository.findAll(Example.of(carryInst));

        return list;
    }

    /**
     * 获取指定日期，指定账号的付款汇总转账金额
     *
     * @param payBankAcc
     * @param ftDate
     * @return
     */
    public BigDecimal paymentTotal(String payBankAcc, Long ftDate) {
        BigDecimal res = BigDecimal.ZERO;
        List<FsFundSettleInst> fsFundCarryInsts = listByPayBankAcc(payBankAcc, ftDate);
        if (CollectionUtils.isEmpty(fsFundCarryInsts)) {
            return res;
        }
        for (FsFundSettleInst fsFundCarryInst : fsFundCarryInsts) {
//            if (Objects.nonNull(fsFundCarryInst.getCarryAmt())) {
//                res = res.add(fsFundCarryInst.getCarryAmt());
//            }
        }
        return res;
    }

    /**
     * 根据收款方银行账号和划款日期查询所有的指令信息
     *
     * @param recBankAcc
     * @param ftDate
     * @return
     */
    public List<FsFundSettleInst> listByRecBankAcc(String recBankAcc, Long ftDate) {
        if (StringUtils.isEmpty(recBankAcc)) {
            return null;
        }
        if (Objects.isNull(ftDate)) {
            return null;
        }

        FsFundSettleInst carryInst = FsFundSettleInst.builder().recBankAcc(recBankAcc).ftDate(ftDate).build();
        List<FsFundSettleInst> list = fsFundSettleInstRepository.findAll(Example.of(carryInst));

        return list;
    }

    /**
     * 获取指定日期，指定账号的收款汇总转账金额
     *
     * @param payBankAcc
     * @param ftDate
     * @return
     */
    public BigDecimal recTotal(String payBankAcc, Long ftDate) {
        BigDecimal res = BigDecimal.ZERO;
        List<FsFundSettleInst> fsFundCarryInsts = listByRecBankAcc(payBankAcc, ftDate);
        if (CollectionUtils.isEmpty(fsFundCarryInsts)) {
            return res;
        }
        for (FsFundSettleInst fsFundCarryInst : fsFundCarryInsts) {
//            if (Objects.nonNull(fsFundCarryInst.getCarryAmt())) {
//                res = res.add(fsFundCarryInst.getCarryAmt());
//            }
        }
        return res;
    }


    /**
     * 分页查找
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<FsFundSettleInst> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "fundCarryInstId";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<FsFundSettleInst> page = fsFundSettleInstRepository.findAll(SpecificationUtil.buildSpecification(param, FsFundSettleInst.class), pageable);

        return page;
    }

    /**
     * 查询所有
     *
     * @return
     */
    private List<FsFundSettleInst> listAll() {
        List<FsFundSettleInst> list = fsFundSettleInstRepository.findAll(Example.of(FsFundSettleInst.builder().build()));
        return list;
    }


    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    public FsFundSettleInst findById(Long id) {
        Optional<FsFundSettleInst> optional = fsFundSettleInstRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }


}
